﻿using System;
using System.Web;
using System.Web.UI.WebControls;

namespace TracNghiemChonTruong.Class
{
    public class ExcelHelper
    {
        public static bool ExportExcel(object dataSource, string filename)
        {
            try
            {
                string attachment = "attachment; filename=" + filename + ".xls";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.AddHeader("content-disposition", attachment);
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
                HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

                using (var stw = new System.IO.StringWriter())
                {
                    using (var htextw = new System.Web.UI.HtmlTextWriter(stw))
                    {
                        using (var gridView = new System.Web.UI.WebControls.GridView { DataSource = dataSource })
                        {
                            gridView.DataBind();
                            gridView.RenderControl(htextw);

                            HttpContext.Current.Response.Write(stw.ToString());
                            HttpContext.Current.Response.End();
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return true;
        }

        public static bool ExportExcel(object dataSource, string filename, string title, string[] line1, string[] line2)
        {
            try
            {
                string attachment = "attachment; filename=" + filename + ".xls";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.AddHeader("content-disposition", attachment);
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
                HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

                using (var stw = new System.IO.StringWriter())
                {
                    using (var htextw = new System.Web.UI.HtmlTextWriter(stw))
                    {
                        using (var table = new Table())
                        {
                            var row = new TableRow();
                            row.Cells.Add(new TableCell() { Text = title, ColumnSpan = 6, CssClass = "title"});
                            table.Rows.Add(row);

                            row = new TableRow();
                            foreach (var cell in line1)
                            {
                                row.Cells.Add(new TableCell() { Text = cell });
                            }
                            table.Rows.Add(row);

                            row = new TableRow();
                            foreach (var cell in line2)
                            {
                                row.Cells.Add(new TableCell() { Text = cell });
                            }
                            table.Rows.Add(row);

                            row = new TableRow();

                            using (var gridView = new System.Web.UI.WebControls.GridView { DataSource = dataSource })
                            {
                                gridView.DataBind();
                                var tableCell = new TableCell();
                                tableCell.ColumnSpan = 6;
                                tableCell.Controls.Add(gridView);
                                row.Cells.Add(tableCell);
                                table.Rows.Add(row);

                                table.RenderControl(htextw);

                                HttpContext.Current.Response.Write(stw.ToString());
                                HttpContext.Current.Response.End();
                            }

                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return true;
        }
    }
}